<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/31
 * Time: 22:34
 */

namespace Admin\Controller;


use Common\Model\GoodsAttrModel;
use Common\Model\GoodsListModel;
use Common\Model\TypeAttrModel;

class GoodsListController extends CommenController
{
	public function index(){
		$gid = I('get.gid');
		$tid = I('get.tid');
		//获取商品类型属性
//		dd($tid);
		$spec =  (new TypeAttrModel())->field('taid,taname')->where("tid=$tid AND class=1")->select();
//		$this->p($spec);
		foreach ($spec as $k => $v){
			$spec[$k]['opt'] = (new GoodsAttrModel())->field('gtid,gtvalue')->where("gid=$gid AND taid={$v['taid']}")->select();
		}
//		$this->p($spec);
		//已添加货品
		$list = (new GoodsListModel())->where("gid=$gid")->select();
		foreach ($list as $k => $v)
		{
			$where = array('gtid' => array('IN', $v['combine']));
			$list[$k]['spec'] = (new GoodsAttrModel())->where($where)->getField('gtvalue', true);
		}
//		$this->p($list);
		$this->assign('spec',$spec);
		$this->assign('list',$list);
        //已添加货品样式总数（不是指库存）
		$goods_num = count($list);
		//货品可能情况数目，如果已添加数目小于可能情况数目，才出现添加货品的输入框
		$possible = 1;
		foreach ($spec as $k => $v) {
			$possible *= count($v['opt']);
		}
		$is_show = $goods_num < $possible ? 1 : 0;
		$this->assign('is_show',$is_show);
//		$this->p($possible);
		$this->display();
	}
	public function add(){
		if (IS_POST){
//			dd($_POST);die;
			if (!$_POST['number'])
			{
//				echo 1;
				$_POST['number'] = date('ymd') . $_POST['gid'] . implode('', $_POST['spec']);
			}
			$_POST['combine'] = implode(',', $_POST['spec']);
//			dd($_POST);die;
			$goods_list = new GoodsListModel();
			if ($goods_list->add($_POST)){
				$this->success('添加成功',U('index',['gid'=>$_POST['gid'],'tid'=>$_POST['tid']]));
				exit;
			}else{
				$this->error('添加失败');exit;
			}
		}
	}
	/**
	 * 编辑货品
	 */
	public function edit(){
		$gid = I('get.gid');
		$tid = I('get.tid');
		$glid = I('get.glid');
		//获取商品类型属性
		$spec =  (new TypeAttrModel())->field('taid,taname')->where("tid=$tid AND class=1")->select();
		foreach ($spec as $k => $v){
			$spec[$k]['opt'] = (new GoodsAttrModel())->field('gtid,gtvalue')->where("gid=$gid AND taid={$v['taid']}")->select();
		}
//		dd($spec);
		$this->assign('spec',$spec);

		//获取旧数据
		$oldData = (new GoodsListModel())->where("glid=$glid")->find();
		$oldData['combine'] =explode(',',$oldData['combine']);
//		$goods_attr = (new GoodsAttrModel())->where("gid=$gid")->field('gtid,gtvalue')->select();
//		dd($goods_attr);die;
		foreach ($oldData['combine'] as $k=>$v){
			$oldData['combine'][$k] = (new GoodsAttrModel())->where("gid=$gid AND gtid={$oldData['combine'][$k]}")->field('gtvalue')->find();

			$oldData['combine'][$k] =$oldData['combine'][$k]['gtvalue'];
		}
//		dd($oldData);
		$this->assign('oldData',$oldData);
		if (IS_POST){
			$_POST['glid'] = $glid;
			$_POST['combine'] = implode(',', $_POST['spec']);
//			dd($_POST);die;
			$stst =(new GoodsListModel())->where("glid=$glid")->save($_POST);
			if ($stst){
//				echo 2;die;
				$this->success('修改成功',U('index',['gid'=>$gid,'tid'=>$tid]));
				exit;
			}else{
//				echo 3;die;
				$this->error('修改失败');
				exit;
			}

		}
     $this->display();
	}
	/**
	 * 删除货品
	 */
	public function del(){
		$tid = I('get.tid');
		$gid = I('get.gid');
//		$this->p($tid);die;
		$glid = I('get.glid');
		$goods_list = new GoodsListModel();
		if ($goods_list->where("glid=$glid")->delete()){
			$this->success('删除成功',U('index',['gid'=>$gid,'tid'=>$tid]));exit;
		}else{
			$this->error('删除失败');exit;
		}

	}
	/**
	 * 异步检测货品是否已存在
	 */
	public function check()
	{
		$where = array('gid' => (int) $_GET['gid'], 'combine' => implode(',', $_GET['spec']));
		echo (new GoodsListModel())->where($where)->count() ? 0 : 1;
//		echo (new GoodsListModel())->where("gid={$_GET['gid']} AND combine=implode(',',{$_GET['spec']})")->count() ? 0 : 1;
	}

}